﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Liger.Web.Common;
using Liger.Web.Common.Extensions;

namespace Liger.Web.UI
{
    public class GridBuilder : UIComponentBuilderBase , IHideObjectMembers
    {
        private Grid Grid
        {
            get
            {
                return this.Component as Grid;
            } 
        }
         

        public GridBuilder()
            : base(new Grid())
        {

        }

        public GridBuilder(Grid grid)
            : base(grid)
        { 

        }
        public GridBuilder Set(Grid grid)
        {
            this.Component = grid;
            return this;
        }
        public GridBuilder Set(Action<Grid> action)
        {
            if (action != null)
            {
                action(this.Grid);
            }
            return this;
        }

        /// <summary>
        /// 设置属性
        /// </summary>
        /// <param name="property"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public GridBuilder Set(string property, object value)
        {
            if (this.Grid.Properties.ContainsKey(property))
            {
                this.Grid.Properties[property] = value;
            }
            else
            {
                this.Grid.Properties.Add(property, value);
            }
            return this;
        }

        /// <summary>
        /// 返回ligerGrid的Manager
        /// </summary>
        /// <param name="manager">manager的对象名</param>
        /// <returns></returns>
        public GridBuilder Manager(string manager)
        {
            this.Grid.Manager = manager;
            return this;
        }

        /// <summary>
        /// 列配置
        /// </summary>
        /// <param name="action"></param>
        /// <returns></returns>
        public GridBuilder Columns(Action<GridColumnsBuilder> action)
        {
            if (action != null)
            {
                action(new GridColumnsBuilder(this.Grid.Columns));
            }
            return this;
        }

        
        /// <summary>
        /// 基础设置
        /// </summary>
        /// <param name="action"></param>
        /// <returns></returns>
        public GridBuilder Setting(Action<GridBaseSettingsBuilder> action)
        {
            if (action != null)
            {
                action(new GridBaseSettingsBuilder(this.Grid.BaseSettings));
            }
            return this;
        }
        /// <summary>
        /// 复选框设置
        /// </summary>
        /// <param name="action"></param>
        /// <returns></returns>
        public GridBuilder Checkbox(Action<GridCheckboxSettingsBuilder> action)
        {
            if (action != null)
            {
                action(new GridCheckboxSettingsBuilder(this.Grid.Checkbox));
            }
            return this;
        }
        /// <summary>
        /// 分组设置
        /// </summary>
        /// <param name="action"></param>
        /// <returns></returns>
        public GridBuilder Groupable(Action<GridGroupingSettingsBuilder> action)
        {
            if (action != null)
            {
                action(new GridGroupingSettingsBuilder(this.Grid.Grouping));
            }
            return this;
        }

        /// <summary>
        /// 是否在本地分页和排序
        /// </summary>
        /// <param name="inLocal"></param>
        /// <returns></returns>
        public GridBuilder Local(bool inLocal)
        {
            if (inLocal)
            {
                Grid.DataAction = GridDataActionType.local;
            }
            else
            {
                Grid.DataAction = GridDataActionType.server;
            }
            return this;
        }
        /// <summary>
        /// 服务器加载数据设置
        /// </summary>
        /// <param name="action"></param>
        /// <returns></returns>
        public GridBuilder Ajax(Action<GridAjaxSettingsBuilder> action)
        {
            if (action != null)
            {
                action(new GridAjaxSettingsBuilder(this.Grid.Paging, this.Grid.Ajax, this.Grid.BaseSettings));
            }
            return this;
        }
        /// <summary>
        /// 分页设置
        /// </summary>
        /// <param name="action"></param>
        /// <returns></returns>
        public GridBuilder Pageable(Action<GridPagingSettingsBuilder> action)
        {
            if (action != null)
            {
                action(new GridPagingSettingsBuilder(this.Grid.Paging,this.Grid.Ajax));
            }
            return this;
        }
         

        /// <summary>
        /// 排序设置
        /// </summary>
        /// <param name="action"></param>
        /// <returns></returns>
        public GridBuilder Sortable(Action<GridSortingSettingsBuilder> action)
        {
            if (action != null)
            {
                action(new GridSortingSettingsBuilder(this.Grid.Sorting));
            }
            return this;
        }
        /// <summary>
        /// 明细表格
        /// </summary>
        /// <param name="keyColumnName">主表主键名</param>
        /// <param name="ajaxParmName">提交参数名</param> 
        /// <param name="action">明细表格构造器</param>
        /// <returns></returns>
        public GridBuilder Detail(string keyColumnName,string ajaxParmName, Action<GridBuilder> action)
        {
            if (action != null)
            {
                var gridBuilder = new GridBuilder();
                action(gridBuilder);

                if (this.Grid.Detail == null)
                    this.Grid.Detail = new GridDetailParm();

                gridBuilder.Ajax(ajax => ajax.AjaxParm(ajaxParmName, () => "rowdata." + keyColumnName));

                this.Grid.Detail.DetailGrid = gridBuilder.Grid;

            }
            return this;
        }


        /// <summary>
        /// 设置为可编辑
        /// </summary>
        /// <returns></returns>
        public GridBuilder Editable()
        {
            return Editable(true);
        }
         /// <summary>
        /// 可编辑设置
         /// </summary>
         /// <param name="value"></param>
         /// <returns></returns>
        public GridBuilder Editable(bool value)
        {
            this.Grid.Editing.Enabled = value;
            return this;
        }

        /// <summary>
        /// 可编辑设置
        /// </summary>
        /// <param name="action"></param>
        /// <returns></returns>
        public GridBuilder Editable(Action<GridEditingSettingsBuilder> action)
        {
            if (action != null)
            {
                action(new GridEditingSettingsBuilder(this.Grid.Editing));
            }
            return this;
        }
        /// <summary>
        /// 列设置
        /// </summary>
        /// <param name="action"></param>
        /// <returns></returns>
        public GridBuilder ColumnSet(Action<GridColumnSettingsBuilder> action)
        {
            if (action != null)
            {
                action(new GridColumnSettingsBuilder(this.Grid.ColumnSettings));
            }
            return this;
        }
        /// <summary>
        /// 行设置
        /// </summary>
        /// <param name="action"></param>
        /// <returns></returns>
        public GridBuilder RowSet(Action<GridRowSettingsBuilder> action)
        {
            if (action != null)
            {
                action(new GridRowSettingsBuilder(this.Grid.RowSettings));
            }
            return this;
        }
        /// <summary>
        /// 汇总设置
        /// </summary>
        /// <param name="action"></param>
        /// <returns></returns>
        public GridBuilder Total(Action<GridTotalSettingsBuilder> action)
        {
            if (action != null)
            {
                action(new GridTotalSettingsBuilder(this.Grid.Total));
            }
            return this;
        }
        /// <summary>
        /// 设置Grid ID
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public GridBuilder Name(string value)
        {
            this.Grid.ID = value;
            return this;
        }
        /// <summary>
        /// 设置Grid 头部
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public GridBuilder Title(string value)
        {
            this.Grid.BaseSettings.Title = value;
            return this;
        }

        /// <summary>
        /// 设置Grid 宽度
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public GridBuilder Width(int value)
        {
            this.Grid.BaseSettings.Width = value.ToString();
            return this;
        }

        /// <summary>
        /// 设置Grid 宽度
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public GridBuilder Width(string value)
        {
            this.Grid.BaseSettings.Width = value;
            return this;
        }
        /// <summary>
        /// 设置Grid 高度
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public GridBuilder Height(int value)
        {
            this.Grid.BaseSettings.Height = value.ToString();
            return this;
        }

        /// <summary>
        /// 设置Grid 高度
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public GridBuilder Height(string value)
        {
            this.Grid.BaseSettings.Height = value;
            return this;
        }

        /// <summary>
        /// 全屏显示
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public GridBuilder FullScreen()
        { 
            this.Grid.BaseSettings.Width = "100%";
            this.Grid.BaseSettings.Height = "100%"; 
            return this;
        }
        /// <summary>
        /// 是否可以拖动
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public GridBuilder Scrollable(bool value)
        {
            this.Grid.BaseSettings.IsScroll = value;
            return this;
        }
        /// <summary>
        /// 设置Grid 数据
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public GridBuilder Data(object data)
        {
            this.Grid.BaseSettings.Data = data; 
            return this;
        }

    }
}
